package com.nercel.dsj.gksales.service.impl;

import com.nercel.dsj.gksales.dao.mapper.ProductMapper;
import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.entity.Product;
import com.nercel.dsj.gksales.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author sunlin
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductServiceImpl implements ProductService {

	private final ProductMapper productMapper;

    @Override
    public void update(Product product) {
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void save(Product productDTO) {
        productMapper.insertSelective(productDTO);
    }

    @Override
    public Product findById(Integer integer) {
        return productMapper.selectByPrimaryKey(integer);
    }

    @Override
    public List<Product> listAll() {
        return productMapper.query(new QueryParams());
    }

    @Override
    public void delete(Integer integer) {
        productMapper.deleteByPrimaryKey(integer);
    }

    @Override
    public List<Product> list(QueryParams queryParams) {
        return productMapper.query(queryParams);
    }

    @Override
    public Product findByUid(String uid) {
        QueryParams queryParams = new QueryParams();
        queryParams.put("uid", uid);
        List<Product> products = list(queryParams);
        return products.size() > 0 ? products.get(0) : null;
    }

}
